package com.example.fw;

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import com.example.tests.ContactData;
import com.example.utils.SortedListOf;


public class ContactHelper extends WebDriverHelperBase{
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false; 
	
	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}
	
	private SortedListOf<ContactData> cachedContacts;
	//---------------------------------------------------------------------------------------------------
	//high level methods
	//---------------------------------------------------------------------------------------------------
	
	public ContactHelper createContact(ContactData contact, boolean fillType) {
		manager.navigateTo().addContactPage();
		fillContactForm(contact,fillType);
	    submitContactCreation();
	    returnHomePage();
	    rebuildCache();
	    return this;
	}
	
	public ContactHelper modifyContact(int index, ContactData contact, boolean fillType) {
		manager.navigateTo().mainPage();
		gotoContactModification(index);
    	fillContactForm(contact, fillType);
		submitContactModification();
		returnHomePage();
		rebuildCache();
	    return this;
	}
	
	public ContactHelper deleteContact(int index) {
		manager.navigateTo().mainPage();
    	gotoContactModification(index);
    	submitContactDelition();
    	returnHomePage();
    	rebuildCache();
	    return this;
	}
	
	public SortedListOf<ContactData> getDbContacts() {
		if
		(cachedContacts == null){
			rebuildCache();
		}
		return cachedContacts;
	}
	
	
	
	private void rebuildCache() {
		cachedContacts = new SortedListOf<ContactData>(manager.getHibernateHelper().listContacts());		
	}

	public SortedListOf<ContactData> getUiContacts() {
		SortedListOf<ContactData> contacts = new SortedListOf<ContactData>();
		manager.navigateTo().mainPage();
		List<WebElement> tableElements = driver.findElements(By.xpath("//tbody[1]/tr[@name='entry']/td[2]"));
		for (WebElement tableElement : tableElements) {
			String title = tableElement.getText();
			contacts.add(new ContactData().withLastname(title));
		}
		return contacts;
	}

	public SortedListOf<ContactData> getUiFullContacts() {
		SortedListOf<ContactData> contacts = new SortedListOf<ContactData>();
		manager.navigateTo().mainPage();
		int number = driver.findElements(By.xpath("//tbody[1]/tr[@name='entry']/td[2]")).size();
		for (int i = 0; i < number; i++) {
			contacts.add(getUiFullContact(i));
		} 
		return contacts;
	}	

	private ContactData getUiFullContact(int index) {
		ContactData contact = new ContactData();
		gotoContactModification(index);
		contact = getContactForm();
		manager.navigateTo().mainPage();
		return contact;
	}

	//---------------------------------------------------------------------------------------------------
	//low level methods
	//---------------------------------------------------------------------------------------------------
	public ContactHelper fillContactForm(ContactData contact, boolean fillType) {

		type(By.name("firstname"), contact.getFirstname());
		type(By.name("lastname"), contact.getLastname());
		type(By.name("address"), contact.getAddress());
		type(By.name("home"), contact.getHome_tel());
		type(By.name("mobile"), contact.getMobile_tel());
		type(By.name("work"), contact.getWork_tel());
		type(By.name("email"), contact.getEmail_1());
		type(By.name("email2"), contact.getEmail_2());
		type(By.name("byear"), contact.getByear());
	    selectByText(By.name("bmonth"),contact.getBmonth());
	    selectByText(By.name("bday"),contact.getBday());
	    if (fillType == CREATION){
	    	selectByText(By.name("new_group"),contact.getNew_group());
	    }
		type(By.name("address2"), contact.getAddress2());
		type(By.name("phone2"), contact.getPhone2());
		return this; 
	}
	
	private ContactData getContactForm() {
		ContactData contact = new ContactData();
		contact.setFirstname(driver.findElement(By.name("firstname")).getAttribute("value"));
		contact.setLastname(driver.findElement(By.name("lastname")).getAttribute("value"));
		contact.setAddress(driver.findElement(By.name("address")).getText());
		contact.setHome_tel(driver.findElement(By.name("home")).getAttribute("value"));
		contact.setMobile_tel(driver.findElement(By.name("mobile")).getAttribute("value"));
		contact.setWork_tel(driver.findElement(By.name("work")).getAttribute("value"));
		contact.setEmail_1(driver.findElement(By.name("email")).getAttribute("value"));
		contact.setEmail_2(driver.findElement(By.name("email2")).getAttribute("value"));
		contact.setByear(driver.findElement(By.name("byear")).getAttribute("value"));
		contact.setBmonth(driver.findElement(By.name("bmonth")).getAttribute("value"));
		contact.setBday(driver.findElement(By.name("bday")).getAttribute("value"));
		contact.setAddress2(driver.findElement(By.name("address2")).getText());
		contact.setPhone2(driver.findElement(By.name("phone2")).getAttribute("value"));
		return contact; 
	}
	
	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		cachedContacts = null;
		return this; 
	}
	
	public ContactHelper gotoContactModification(int index) {
		click(By.xpath("(//img[@alt='Edit'])[" + (index + 1) + "]"));
		return this; 
	}

	public ContactHelper submitContactDelition() {
		click(By.xpath("//input[2][@name='update']"));
		cachedContacts = null;
		return this; 
	}
	
	public ContactHelper submitContactModification() {
		click(By.xpath("//input[11][@name='update']"));
		cachedContacts = null;
		return this; 
	}
	
	public ContactHelper returnHomePage() {
		click(By.linkText("home page"));
		return this; 
	}


}
